package com.shenfa.action.systemManager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.bank.sdb.util.encryption.PasswordEncryption;
import com.opensymphony.xwork2.ModelDriven;
import com.shenfa.action.BaseAction;
import com.shenfa.action.upload.DownloadAction;
import com.shenfa.bean.Emprole;
import com.shenfa.bean.Person;
import com.shenfa.service.systemManager.person.PersonService;

import com.shenfa.util.ResultObject;
/**
 * 用户
 * @author jiagnmaochun
 *
 */
public class PersonAction extends BaseAction implements ModelDriven<Person>{
	
	private static final long serialVersionUID = 1L;
	
	private static Logger log=Logger.getLogger(DownloadAction.class);
	private Emprole role;
	private String loginname;
	private HttpServletRequest request;
	private String oldpwd;
	Person person = new Person();
	
	public void setPerson(Person person) {
		this.person = person;
	}
	public Person getModel() {
		return person;
	}

	public Emprole getRole() {
		return role;
	}
	public void setRole(Emprole role) {
		this.role = role;
	}

	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	
	//分页
	private int start;
	private int limit;

	@Autowired
	private PersonService personService;
	/**
	 * 添加一个用户
	 * @return
	 */
	public String add(){
		log.debug("depName="+person.getDepName()+",depNo="+person.getDepNo()+",duty="+person.getDuty()+",email="+person.getEmail()
				+",id="+person.getId()+",loginName="+person.getLoginName()+",mobile="+person.getMobile()+",name="+person.getName()
				+",orgName="+person.getOrgName()+",orgNo="+person.getOrgNo()+",password="+person.getPassword()+",phone="+person.getPhone()
				+",remark="+person.getRemark()+",state="+person.getState());
		personService.add(person);
		result=ResultObject.build("0",0);
		return SUCCESS;
	}
	/**
	 * 修改一个用户
	 * @return
	 */
	public String update(){
		log.debug("depName="+person.getDepName()+",depNo="+person.getDepNo()+",duty="+person.getDuty()+",email="+person.getEmail()
				+",id="+person.getId()+",loginName="+person.getLoginName()+",mobile="+person.getMobile()+",name="+person.getName()
				+",orgName="+person.getOrgName()+",orgNo="+person.getOrgNo()+",password="+person.getPassword()+",phone="+person.getPhone()
				+",remark="+person.getRemark()+",state="+person.getState());
		String psd =person.getPassword();
		if(null != psd && !"".equals(psd)){
			person.setPassword(PasswordEncryption.encrypt(psd));
		}
		personService.update(person);
		result=ResultObject.build("0",0);
		return SUCCESS;
	}
	
	/**
	 * 修改密码
	 * 
	 * @return
	 */
	public String editPwd(){
		request = ServletActionContext.getRequest();
		Person per = (Person)request.getSession().getAttribute("login");
		person.setId(per.getId());	
		String nepwd = PasswordEncryption.encrypt(person.getPassword());
		person.setPassword(nepwd);
		int a = personService.logStateUp(person);
		if(a>0){
			per.setPassword(nepwd);
			request.getSession().setAttribute("login",per);
			result=ResultObject.build("0","修改成功!");				
		}else{
			result=ResultObject.build("1","修改失败!");
		}
		return SUCCESS;
	}
	
	/**
	 * 密码验证
	 * 
	 * @return
	 */
	public String checkPwd(){
		request = ServletActionContext.getRequest();
		Person per = (Person)request.getSession().getAttribute("login");
		//session中的密码
		String psd =per.getPassword();
		if(!PasswordEncryption.verifyPassword(psd,oldpwd)){
			result=ResultObject.build("1","密码输入错误,请重新输入!");
		}else{
			result=ResultObject.build("0",0);			
		}
		return SUCCESS;
	}
	
	/**
	 * 删除一个用户
	 * @return
	 */
	public String del(){
		log.debug("personNo="+person.getId());
		role=new Emprole();
		role.setPid(person.getId());
		//获取用户的角色数量
		String flag=personService.checkDelPerson(role);
		//如果没有角色
		if("0".equals(flag)){
			personService.del(person);
			result=ResultObject.build("0","删除成功！");
		}else{
			result=ResultObject.build("-1","删除失败！该用户被分配了角色不能删除！");
		}
		return SUCCESS;
	}
	/**
	 * 查询所有用户
	 * @return
	 */
	public String listAll(){
		List<Person> list = personService.getAll();
		result=ResultObject.build("0",list);
		return SUCCESS;
	}
	
	/**
	 * 查询部门成员的完整信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listByDepart(){
		if(null != person){
			log.debug("departNo="+person.getDepNo());
			List<Person> list=personService.listByDepart(person,start,limit);
			result=ResultObject.build("0",list);
			result.put("total", personService.queryCount(person));
		}else{
			return null;
		}
		return SUCCESS;
	}
	/**
	 * 验证用户是否被赋予了角色
	 * @return
	 */
	public String checkDelPerson(){
		String flag = personService.checkDelPerson(role);
		if("0".equals(flag)){
			result = ResultObject.build("0", null);
		}else{
			result = ResultObject.build("1", null);
		}
		return SUCCESS;
	}
	/**
	 * 验证用户名是否存在 
	 * @return
	 */
	public String checkPerson(){
		int flag = personService.checkPerson(loginname);
		if(flag == 0){
			result = ResultObject.build("0", null);//不存在
		}else if(flag > 0){
			result = ResultObject.build("1", null);//存在
		}else{
			result = ResultObject.build("2", null);//查询异常
		}
		return SUCCESS;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}
}
